package pl.edu.pk.adcampaignfacebook.controller;

import java.util.ArrayList;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.adcampaignfacebook.integration.FeedMap;
import pl.edu.pk.adcampaignfacebook.integration.IntegrationPhoto;
import pl.edu.pk.adcampaignfacebook.rest.responses.CommentResponse;
import pl.edu.pk.adcampaignfacebook.rest.responses.LoginResponse;
import pl.edu.pk.adcampaignfacebook.rest.responses.PhotoResponse;
import pl.edu.pk.adcampaignfacebook.rest.responses.RestComment;
import pl.edu.pk.adcampaignfacebook.rest.responses.StatusCodes;
import pl.edu.pk.adcampaignfacebook.service.AccountService;

/**
 * Created with IntelliJ IDEA.
 * User: Piotrek
 * Date: 28.05.12
 * Time: 02:28
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping("/api")
public class RestController {

	 @Autowired
	 AccountService accountService;

    @RequestMapping(value = "/login")
    public @ResponseBody
    LoginResponse loginUser(@RequestParam(value = "login") String login,
                            @RequestParam(value="password") String password) {

        Md5PasswordEncoder mdp = new Md5PasswordEncoder();
        String encrypted = mdp.encodePassword(password,null);

        UserDetails user = accountService.loadUserByUsername(login);

        String token =  mdp.encodePassword(login, new  Random().nextLong());
        if(user!=null && user.getPassword().equals(encrypted)){

//           memory.addRestUser(token,user);

           return new LoginResponse(StatusCodes.OK,token);

        }else {
            return new LoginResponse(StatusCodes.OK,token);
        }
    }
    
	
    @RequestMapping(value = "/addPhoto")
    public @ResponseBody
    PhotoResponse addPhoto(@RequestParam(value = "token") String token,
                    @RequestParam(value = "url") String url,
                    @RequestParam(value = "title") String title,
                    @RequestParam(value = "description") String description) {

    	IntegrationPhoto integrationPhoto = new IntegrationPhoto();
    	integrationPhoto.setDescription(description);
    	integrationPhoto.setTitle(title);
    	integrationPhoto.setUrl(url);
    	
    	FeedMap.addPhoto("admin", integrationPhoto);
    	
    	return new PhotoResponse(StatusCodes.OK, "-1");
    	
    }

	
    @RequestMapping(value = "/getComments")
    public @ResponseBody
    CommentResponse getComments(@RequestParam(value = "token") String token,
                    @RequestParam(value = "message_id") Long id) {
            return new CommentResponse(StatusCodes.OK, new ArrayList<RestComment>());
    }

}
